package com.xbniao.owms.utils.barcode;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class BoxBarCodeParamByTemplateCode {

	private float _table_width = PageSize.A4.getWidth();
	private float _bar_code_width = 0f;
	private float _bar_code_height = 36f;
	private float _bar_code_font_size = 14f;
	
	public BoxBarCodeParamByTemplateCode(String code)
	{
		this._table_width = this.getMainTableWidth(code);
		this._bar_code_height = this.getBarCodeHeight(code);
		this._bar_code_width = this.getBarCodeWidthSizeByCode(code);
		this._bar_code_font_size = this.getBarCodeFontSize(code);
	}
	
	public float get_table_width() {
		return _table_width;
	}
	public void set_table_width(float _table_width) {
		this._table_width = _table_width;
	}

	public float get_bar_code_width() {
		return _bar_code_width;
	}

	public void set_bar_code_width(float _bar_code_width) {
		this._bar_code_width = _bar_code_width;
	}

	public float get_bar_code_height() {
		return _bar_code_height;
	}

	public void set_bar_code_height(float _bar_code_height) {
		this._bar_code_height = _bar_code_height;
	}
	
	public float get_bar_code_font_size() {
		return _bar_code_font_size;
	}

	public void set_bar_code_font_size(float _bar_code_font_size) {
		this._bar_code_font_size = _bar_code_font_size;
	}

	/**
	 * 设置document距离A4纸的边框的距离
	 * @param code
	 * @return
	 */
	public static Document getTemplateBorder(String code)
	{
		Document document = null;
		switch (code) {
		case "8":
			document = new Document(PageSize.A4,24.0945f,24.0945f,8.5039f,8.5039f);
			break;
		case "9":
			document = new Document(PageSize.A4,5.6693f,5.6693f,4.2520f,4.2520f);
			break;
		default:
			document = new Document(PageSize.A4,0,0,0,0);
			break;
		}
		return document;
	}

	/**
	 * 设置表格宽度
	 * @param code
	 * @return  单位：磅
	 */
	public static float getMainTableWidth(String code)
	{
		float returnFloat = PageSize.A4.getWidth();
		
		switch (code) {
		case "8":
			returnFloat = PageSize.A4.getWidth() - (2 * 24.0945f);
			break;
		case "9":
			returnFloat = PageSize.A4.getWidth() - (2 * 5.6693f);
			break;
		}
		
		return returnFloat;
	}
	
	/**
	 * 获取生成条码的高度
	 * @return
	 */
	public static float getBarCodeHeight(String code)
	{
		float barCodeHeight = 0;
		
		switch (code) {
		case "8":
			barCodeHeight = 48.1890f;
			break;
		case "9":
			barCodeHeight = 60.6929f;
			break;
		}
		
		return barCodeHeight;
	}
	
	/**
	 * 获取生成条码的宽度
	 * @param code
	 * @return
	 */
	public static float getBarCodeWidthSizeByCode(String code)
	{
		float barCodeWidth = 0;
		
		switch (code) {
		case "8":
			barCodeWidth = 235.2756f;
			break;
		case "9":
			barCodeWidth = 249.4488f;
			break;
		}
		
		return barCodeWidth;
	}
	
	/**
	 * 设置条码下方字体的大小
	 * @param code
	 * @return
	 */
	public static float getBarCodeFontSize(String code)
	{
		float fontSize = 10f;
		
		switch (code) {
		case "8":
			fontSize = 14f;
			break;
		case "9":
			fontSize = 14f;
			break;
		}
		
		return fontSize;
	}
	
	/**
	 * 创建一个基本字体
	 * @return
	 */
	public static BaseFont getBaseFont(boolean flag) 
	{
		BaseFont bfChinese = null;
		if(flag)
		{
			try {
		 		bfChinese = BaseFont.createFont(BaseFont.HELVETICA,
		 		          BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
			} catch (Exception e) {
			 	 e.printStackTrace();
			}
		}
		else
		{
			try {
				bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",false);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
		return bfChinese;
	}
	/**
	 * 获取Receipt Id和code的字体
	 * @param code
	 * @return
	 */
	public static Font getBorCodePartFontByTemplateCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(true),12,Font.BOLD);
			break;
		case "9":
			font = new Font(getBaseFont(true),13,Font.BOLD);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 获取第一部分“小笨鸟标识、收货预报货主”的字体
	 * @param code
	 * @return
	 */
	public static Font getFirstPartFontByCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(false),15,Font.BOLD);
			break;
		case "9":
			font = new Font(getBaseFont(false),20,Font.BOLD);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 获取第一部分“小笨鸟标识、收货预报货主”的字体
	 * @param code
	 * @return
	 */
	public static Font getFourPartFontByCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(false),15,Font.BOLD);
			break;
		case "9":
			font = new Font(getBaseFont(false),18,Font.BOLD);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 获取第二部分左边title的字体
	 * @param code
	 * @return
	 */
	public static Font getSecondPartLeftFontByCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(false),12,Font.BOLD);
			break;
		case "9":
			font = new Font(getBaseFont(false),13,Font.BOLD);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 获取第二部分右边text的字体
	 * @param code
	 * @return
	 */
	public static Font getSecondPartRigthFontByCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(false),12,Font.NORMAL);
			break;
		case "9":
			font = new Font(getBaseFont(false),13,Font.NORMAL);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 获取最后一部分title的字体
	 * @param code
	 * @return
	 */
	public static Font getEndPartTitleFontByCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(false), 12, Font.BOLD);
			break;
		case "9":
			font = new Font(getBaseFont(false), 12, Font.BOLD);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 获取最后一部分text的字体
	 * @param code
	 * @return
	 */
	public static Font getEndPartTextFontByCode(String code)
	{
		Font font = null;
		switch (code) {
		case "8":
			font = new Font(getBaseFont(false),12,Font.NORMAL);
			break;
		case "9":
			font = new Font(getBaseFont(false),12,Font.NORMAL);
			break;
		default:
			break;
		}
		return font;
	}
	
	/**
	 * 设置第二部分table的同一行cell的比例
	 * @param code
	 * @return
	 */
	public static float[] getSencodPdfPTabelCellProportion(String code)
	{
		switch (code) {
		case "8":
			return new float[]{0.26f,0.74f};
		case "9":
			return new float[]{0.27f,0.73f};
		default:
			return new float[]{};
		}
		
	}
	
	/**
	 * 设置最后一部分table的同一行cell的比例
	 * @param code
	 * @return
	 */
	public static float[] getEndPdfFTableCellProportion(String code)
	{
		switch (code) {
		case "8":
			return new float[]{0.22f,0.35f,0.15f,0.28f};
		case "9":
			return new float[]{0.22f,0.33f,0.15f,0.30f};
		default:
			return new float[]{};
		}
	}
	
	/**
	 * 隐藏cell的所有边框
	 * @param cell
	 */
	public static void setHideAllBorders(PdfPCell cell)
	{
		cell.disableBorderSide(1);
		cell.disableBorderSide(2);
		cell.disableBorderSide(4);
		cell.disableBorderSide(8);
	}
	
	/**
	 * 保留cell的底部边框
	 * @param cell
	 */
	public static void setHideTopLeftRigthBorders(PdfPCell cell)
	{
		cell.disableBorderSide(1);
		cell.disableBorderSide(4);
		cell.disableBorderSide(8);
	}
	
	/**
	 * 生成Receipt ID 的cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellReceiptId(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"receiptId");
		return msgGcell;
	}
	
	/**
	 * 生成 barCode 的cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellBarCode(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"barCode");
		return msgGcell;
	}
	
	/**
	 * 生成 第一部分 的cell（从上到下第一条横线以上的部分）
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellFirstPart(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"firstPart");
		return msgGcell;
	}
	
	/**
	 * 生成 第二部分 的cell（从上到下第二条横线以上第一条线以下的部分），第一行
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellSecondPartFirstRowLeft(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"SecondPartFirstRowLeft");
		return msgGcell;
	}
	/**
	 * 生成 第二部分 的cell（从上到下第二条横线以上第一条线以下的部分），第一行
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellSecondPartFirstRowRight(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"SecondPartFirstRowRight");
		return msgGcell;
	}
	
	/**
	 * 生成 第二部分 的cell（从上到下第二条横线以上第一条线以下的部分），第二，三行    左边cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellSecondPartMiddleRowLeft(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"SecondPartMiddleRowLeft");
		return msgGcell;
	}
	
	/**
	 * 生成 第二部分 的cell（从上到下第二条横线以上第一条线以下的部分），第二，三行  右边cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellSecondPartMiddleRowRight(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"SecondPartMiddleRowRight");
		return msgGcell;
	}
	
	/**
	 * 生成 第二部分 的cell（从上到下第二条横线以上第一条线以下的部分），最后一行    左边cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellSecondPartEndRowLeft(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"SecondPartEndRowLeft");
		return msgGcell;
	}
	
	/**
	 * 生成 第二部分 的cell（从上到下第二条横线以上第一条线以下的部分），最后一行     右边cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellSecondPartEndRowRight(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"SecondPartEndRowRight");
		return msgGcell;
	}
	
	/**
	 * 生成最后一部分第一个cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellEndPartFirstCell(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"EndPartFirstCell");
		return msgGcell;
	}
	
	/**
	 * 生成最后一部分第二个cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellEndPartSencodCell(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"EndPartSencodCell");
		return msgGcell;
	}
	
	/**
	 * 生成最后一部分第三个cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellEndPartThridCell(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"EndPartThridCell");
		return msgGcell;
	}
	
	/**
	 * 生成最后一部分第四个cell
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCellEndPartEndCell(Paragraph graph,int align, boolean isHideAllBorders,String code)
	{
		PdfPCell msgGcell = createPdfCell(graph, align, isHideAllBorders, code);
		setCellPadding(msgGcell,code,"EndPartEndCell");
		return msgGcell;
	}
	
	/**
	 * 生成Cell的基本方法，要想打印边线，在此方法设置即可
	 * @param graph
	 * @param align
	 * @param isHideAllBorders
	 * @param code
	 * @return
	 */
	public static PdfPCell createPdfCell (Paragraph graph,int align, boolean isHideAllBorders,String code){
		PdfPCell msgGcell = new PdfPCell(graph);
		msgGcell.setVerticalAlignment(Element.ALIGN_TOP);
		msgGcell.setHorizontalAlignment(align);
		msgGcell.setUseAscender(true);
		msgGcell.setUseDescender(true);
		if (isHideAllBorders)
			msgGcell.setBorder(PdfPCell.NO_BORDER);
		else
			setHideTopLeftRigthBorders(msgGcell);
		return msgGcell;
    }
	
	/**
	 * 设置生成的Cell距离上下边距的大小
	 * @param msgGcell
	 * @param code
	 * @param cellName   标识生成的cell是哪个cell，来确定设置的大小
	 */
	public static void setCellPadding(PdfPCell msgGcell,String code,String cellName)
	{
		if("receiptId".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingTop(9.8425f);
				msgGcell.setPaddingBottom(7.8425f);
				break;
			case "9":
				msgGcell.setPaddingTop(25.0000f);
				msgGcell.setPaddingBottom(11.8425f);
				break;
			}
		}
		else if("barCode".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingTop(3f);
				msgGcell.setPaddingBottom(5f);
				break;
			case "9":
				msgGcell.setPaddingTop(5f);
				msgGcell.setPaddingBottom(14.8425f);
				break;
			}
		}
		else if("firstPart".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingLeft(17.0000f);
				msgGcell.setPaddingRight(17.0000f);
				msgGcell.setPaddingTop(10.8425f);
				msgGcell.setPaddingBottom(10.8425f);
				break;
			case "9":
				msgGcell.setPaddingLeft(19.8425f);
				msgGcell.setPaddingRight(19.8425f);
				msgGcell.setPaddingTop(23.8425f);
				msgGcell.setPaddingBottom(17.8425f);
				break;
			}
		}
		else if("SecondPartFirstRowLeft".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingLeft(15.7000f);
				msgGcell.setPaddingTop(6.0000f);
				break;
			case "9":
				msgGcell.setPaddingLeft(17.8425f);
				msgGcell.setPaddingTop(15.0000f);
				msgGcell.setPaddingBottom(7f);
				break;
			}
		}
		else if("SecondPartFirstRowRight".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingRight(17.0000f);
				msgGcell.setPaddingTop(6.0000f);
				break;
			case "9":
				msgGcell.setPaddingRight(19.8425f);
				msgGcell.setPaddingTop(15.0000f);
				msgGcell.setPaddingBottom(7f);
				break;
			}
		}
		else if("SecondPartMiddleRowLeft".equals(cellName))
		{
			switch (code) {
			case "8":
				break;
			case "9":
				msgGcell.setPaddingTop(6.0000f);
				msgGcell.setPaddingBottom(6f);
				break;
			}
		}
		else if("SecondPartMiddleRowRight".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingRight(17.0000f);
				break;
			case "9":
				msgGcell.setPaddingTop(6.0000f);
				msgGcell.setPaddingBottom(6f);
				msgGcell.setPaddingRight(19.8425f);
				break;
			}
		}
		else if("SecondPartEndRowLeft".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingBottom(5.0000f);
				break;
			case "9":
				msgGcell.setPaddingTop(6.0000f);
				msgGcell.setPaddingBottom(18.0000f);
				break;
			}
		}
		else if("SecondPartEndRowRight".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingRight(17.0000f);
				msgGcell.setPaddingBottom(5.0000f);
				break;
			case "9":
				msgGcell.setPaddingRight(19.8425f);
				msgGcell.setPaddingTop(6.0000f);
				msgGcell.setPaddingBottom(18.0000f);
				break;
			}
		}
		else if("EndPartFirstCell".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingLeft(17.0000f);
				msgGcell.setPaddingTop(13.8425f);
				msgGcell.setPaddingBottom(13.8425f);
				break;
			case "9":
				msgGcell.setPaddingLeft(19.8425f);
				msgGcell.setPaddingTop(23.8425f);
				msgGcell.setPaddingBottom(23.8425f);
				break;
			}
		}
		else if("EndPartSencodCell".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingTop(13.8425f);
				msgGcell.setPaddingBottom(13.8425f);
				break;
			case "9":
				msgGcell.setPaddingTop(23.8425f);
				msgGcell.setPaddingBottom(23.8425f);
				break;
			}
		}
		else if("EndPartThridCell".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingTop(13.8425f);
				msgGcell.setPaddingBottom(13.8425f);
				break;
			case "9":
				msgGcell.setPaddingTop(23.8425f);
				msgGcell.setPaddingBottom(23.8425f);
				break;
			}
		}
		else if("EndPartEndCell".equals(cellName))
		{
			switch (code) {
			case "8":
				msgGcell.setPaddingRight(17.0000f);
				msgGcell.setPaddingTop(10.8425f);
				break;
			case "9":
				msgGcell.setPaddingRight(19.8425f);
				msgGcell.setPaddingTop(17.8425f);
				break;
			}
		}
	}
	
}
